//Send a MIDI note-on message.
void noteOn(byte channel, byte note, byte attack_velocity) {
  talkMIDI( (0x90 | channel), note, attack_velocity);
}

//Send a MIDI note-off message.
void noteOff(byte channel, byte note, byte release_velocity) {
  talkMIDI( (0x80 | channel), note, release_velocity);
}

//Sends a MIDI message
void talkMIDI(byte cmd, byte data1, byte data2) {
  mySerial.write(cmd);
  mySerial.write(data1);

  //Since some commands do not have 2 data bytes we need to send the second one only sometimes. 
  //since we use only 0x8, 0x9, 0xB and 0xC status nibbles and 0xC is the only one that does not have
  //a 2nd data byte, we can simply ask if the first nibble is less than or equal to 0xB. The &F0 passes
  //all the status nibble bits thyrough while zeroing the channel nibble for easier comparison. 
  if( (cmd & 0xF0) <= 0xB0)
    mySerial.write(data2);
}
